import os.path
import pickle
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver import ChromeOptions
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

from time import sleep

from selenium.webdriver.support.wait import WebDriverWait

option = ChromeOptions()
option.add_argument('--start-maximized')  # 最大化窗口
option.add_experimental_option('excludeSwitches', ['enable-automation'])  # 禁用自动化栏
option.add_experimental_option('useAutomationExtension', False)  # 禁用自动化栏的原理：将window.navigator.webdriver改为undefined。

# 屏蔽密码提示框
prefs = {
    'credentials_enable_service': False, 'profile.password_manager_enabled': False
}
option.add_experimental_option('prefs', prefs)

# 反爬虫特征处理
option.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=option)
wait_dirver = WebDriverWait(driver, 30)
driver.get(
    'https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm%3Fspm%3Da21bo.jianhua.754894437.1.5af92a89zyJDZq%26pm_id%3D1501036000a02c5c3739')

action = ActionChains(driver)

if not os.path.exists("session.pkl"):
    time.sleep(5)
    with open("session.pkl", "wb")as file:
        pickle.dump(driver.get_cookies(), file)
else:
    with open("session.pkl", "rb")as file:
        cookies = pickle.load(file)
        for cookie in cookies:
            driver.add_cookie(cookie)


driver.refresh()

driver.get("https://www.taobao.com/")


windows = driver.window_handles
driver.switch_to.window(windows[-1])
# 等待元素可点击
upload_button = wait_dirver.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="J_Search"]/div[2]/div[1]')))
upload_button.click()
upload_image = wait_dirver.until(EC.presence_of_element_located((By.XPATH, '//*[@id="image-search-custom-file-input"]')))


# 上传图片
upload_image.send_keys(r'D:\taobao\O1CN01meeLKh1bFHRgji23t_!!0-item_pic.jpg_Q75.jpg')

search = driver.find_element(By.XPATH, '//*[@id="image-search-upload-button"]')
search.click()

windows = driver.window_handles
driver.switch_to.window(windows[-1])
time.sleep(2)
wrapper_ele = wait_dirver.until(EC.presence_of_element_located((By.XPATH, '//*[@id="pageContent"]/div[1]/div[2]/div/div')))
goods_ele = wrapper_ele.find_elements(By.XPATH, "div")
for goods in goods_ele:
    time.sleep(2)
    windows = driver.window_handles
    driver.switch_to.window(windows[-1])
    goods.click()
    time.sleep(2)
    windows = driver.window_handles
    driver.switch_to.window(windows[-1])

    goods_images_wrapper = wait_dirver.until(EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/div[2]/div[1]/div[2]/div/div[1]/div[1]/ul')))
    goods_images = goods_images_wrapper.find_elements(By.XPATH, "li")

    for goods_image in goods_images:
        goods_image_ele = goods_image.find_element(By.XPATH, "img")
        image_src = goods_image_ele.get_attribute("src")

        if image_src:
            image_src = image_src.replace("_.webp", "")
        time.sleep(2)
        print("正在下载图片 ==》 %s" % image_src)

        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    driver.close()


sleep(5)
driver.quit()